﻿namespace MainGame.GameScreens
{
    using System;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    using XGameLibrary.Controls;
    using XGameLibrary.Input;
    using XGameLibrary.ScreenManager;

    public class CharacterGeneratorScreen : BaseGameState
    {
        private string[] genderItems = { "Male", "Female" };
        private string[] hairItemsMale = { "Purple", "Blond" };
        private string[] hairItemsFemale = { "Purple" };

        private LeftRightSelector genderSelector;
        private LeftRightSelector hairSelector;
        private PictureBox backgroundImage;
        private QuestionBox femaleCharacter;

        private PictureBox characterImage;
        Texture2D[,] characterImages;

        public CharacterGeneratorScreen(Game game, GameStateManager stateManager) :
            base(game, stateManager)
        {
        }

        public string SelectedGender
        {
            get
            {
                return genderSelector.SelectedItem;
            }
        }
        public string SelectedHair
        {
            get
            {
                return hairSelector.SelectedItem;
            }
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            this.LoadImages();
            this.CreateControls();
            this.FemaleLoad();
        }

        public override void Update(GameTime gameTime)
        {
            this.ControlManager.Update(gameTime);
            base.Update(gameTime);

            if (this.femaleCharacter.Visible == true)
            {
                if (InputHandler.KeyReleased(Keys.Escape))
                {
                    this.femaleCharacter.Visible = false;
                    this.genderSelector.TabStop = true;
                    this.hairSelector.TabStop = true;
                    this.ControlManager[4].TabStop = true;
                }
                else
                {
                    return;
                }
            }

            if (this.genderSelector.SelectedItem == "Female")
            {
                this.hairSelector.SetItems(this.hairItemsFemale, 125);
                this.hairSelector.SelectedIndex = 0;
                this.genderSelector.SelectedIndex = 1;
            }
            else
            {
                this.hairSelector.SetItems(this.hairItemsMale, 125);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            this.GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);

            this.ControlManager.Draw(this.GameRef.SpriteBatch);

            this.GameRef.SpriteBatch.End();
        }

        private void CreateControls()
        {
            Texture2D leftTexture = this.GameRef.Content.Load<Texture2D>(@"GUI\Menu\leftarrowUp");
            Texture2D rightTexture = this.GameRef.Content.Load<Texture2D>(@"GUI\Menu\rightarrowUp");
            Texture2D stopTexture = this.GameRef.Content.Load<Texture2D>(@"GUI\Menu\StopBar");

            this.backgroundImage = new PictureBox(
                this.GameRef.Content.Load<Texture2D>(@"Images\Backgrounds\MainMenuScreen"),
                this.GameRef.ScreenRectangle);
            this.ControlManager.Add(this.backgroundImage);

            Label label = new Label();
            label.Text = "Create your character";
            label.Size = label.SpriteFont.MeasureString(label.Text);
            label.Position = new Vector2((this.GameRef.Window.ClientBounds.Width - label.Size.X) / 2, 25);
            this.ControlManager.Add(label);

            this.genderSelector = new LeftRightSelector(leftTexture, rightTexture, stopTexture);
            this.genderSelector.SetItems(this.genderItems, 125);
            this.genderSelector.Position = new Vector2(label.Position.X + -115, 75);
            this.genderSelector.SelectionChanged += new EventHandler(this.SelectionChanged);
            this.ControlManager.Add(this.genderSelector);

            this.hairSelector = new LeftRightSelector(leftTexture, rightTexture, stopTexture);
            this.hairSelector.SetItems(this.hairItemsMale, 125);
            this.hairSelector.Position = new Vector2(label.Position.X + 175, 75);
            this.hairSelector.SelectionChanged += this.SelectionChanged;
            this.ControlManager.Add(this.hairSelector);

            LinkLabel linkLabel = new LinkLabel();
            linkLabel.Text = "Create character";
            linkLabel.Position = new Vector2(label.Position.X + 25, 680);
            linkLabel.Selected += new EventHandler(this.LinkLabel_Selected);
            this.ControlManager.Add(linkLabel);

            this.characterImage = new PictureBox(
                this.characterImages[0, 0],
                new Rectangle(620, 590, 31, 64),
                new Rectangle(0, 0, 31, 64));
            this.ControlManager.Add(this.characterImage);

            this.ControlManager.NextControl();
        }

        private void LoadImages()
        {
            this.characterImages = new Texture2D[this.genderItems.Length, this.hairItemsMale.Length];

            for (int i = 0; i < genderItems.Length; i++)
            {
                for (int j = 0; j < hairItemsMale.Length; j++)
                {
                    try
                    {
                        this.characterImages[i, j] = this.GameRef.Content.Load<Texture2D>(@"Images\Sprites\"
                            + genderItems[i] + hairItemsMale[j]);
                    }
                    catch
                    {
                    }
                }
            }
        }

        private void LinkLabel_Selected(object sender, EventArgs e)
        {
            if (this.SelectedGender == "Female")
            {
                this.femaleCharacter.Visible = true;
                foreach (Control ctrl in this.ControlManager)
                {
                    ctrl.TabStop = false;
                }
                return;
            }

            InputHandler.Flush();

            this.StateManager.PopState();
            this.StateManager.PushState(this.GameRef.GamePlayScreen);
        }

        private void SelectionChanged(object sender, EventArgs e)
        {
            if (this.genderSelector.SelectedItem == "Female")
            {
                this.characterImage.Image = characterImages[1, 0];
            }
            else
            {
                this.characterImage.Image = characterImages[genderSelector.SelectedIndex, hairSelector.SelectedIndex];
            }
        }

        private void FemaleLoad()
        {
            Texture2D box = this.GameRef.Content.Load<Texture2D>(@"GUI\questionBox");
            Label question = new Label();
            question.Text = "A girl in 2S? Don't make me laugh";
            question.Color = Color.Black;

            LinkLabel option1 = new LinkLabel();
            option1.Text = "OK :(";
            option1.Color = Color.Black;

            LinkLabel[] options = new LinkLabel[] { option1 };

            this.femaleCharacter = new QuestionBox(box, question, options);

            this.femaleCharacter.Visible = false;
            this.ControlManager.Add(this.femaleCharacter);
        }
    }
}
